package com.youyunsoft.commons.recyclerview_expandlistview;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.youyunsoft.commons.BaseActivity;
import com.youyunsoft.commons.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerExpandActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    RecyclerView recyclerViewParent;

    private void initView() {

        recyclerViewParent = bindView(R.id.recycler_parent);
        recyclerViewParent.setLayoutManager(new LinearLayoutManager(this));

        ArrayList<ParentBean> data = new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            ParentBean bean = new ParentBean();
            bean.setName("Parent " + i);
            ArrayList<ChildBean> child = new ArrayList<>();
            ChildBean childBean = new ChildBean();
            childBean.setName("Child 1 " + i);
            child.add(childBean);
            childBean = new ChildBean();
            childBean.setName("Child 2 " + i);
            child.add(childBean);

            bean.setChildList(child);
            data.add(bean);
        }


        ParentAdapter adapter = new ParentAdapter(data);
        recyclerViewParent.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }


    class ParentAdapter extends RecyclerView.Adapter {

        private List<ParentBean> mDatas = new ArrayList<>();
        View childView;

        public ParentAdapter(List<ParentBean> data) {
            mDatas.addAll(data);
            childView = View.inflate(RecyclerExpandActivity.this, R.layout.activity_recycler_main_list_item_child, null);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View itemView = LayoutInflater.from(RecyclerExpandActivity.this)
                    .inflate(R.layout.activity_recycler_expand_list_item, parent,false);

            ParentHolder holder = new ParentHolder(itemView);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            final ParentHolder vh = (ParentHolder) holder;
            final ParentBean bean = mDatas.get(position);

            ViewGroup.LayoutParams layoutParams = vh.itemView.getLayoutParams();
            layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;


            vh.tv_parent.setText(bean.getName());

            ChildAdapter childAdapter = new ChildAdapter(bean.getChildList());
            vh.recyclerViewChild.setAdapter(childAdapter);


            if (bean.isExpand()) {
                vh.recyclerViewChild.setVisibility(View.VISIBLE);
            } else {
                vh.recyclerViewChild.setVisibility(View.GONE);
            }

            vh.layout_layout_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bean.setExpand(!bean.isExpand());
                    notifyItemChanged(position);

                    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    vh.recyclerViewChild.measure(w, h);
                    int height = vh.recyclerViewChild.getMeasuredHeight();
                    if(bean.isExpand()&&position==mDatas.size()-1){
                        recyclerViewParent.smoothScrollBy(0, height);
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }
    }

    class ChildAdapter extends RecyclerView.Adapter {

        private List<ChildBean> mDatas = new ArrayList<>();

        public ChildAdapter(List<ChildBean> mDatas) {
            this.mDatas = mDatas;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(RecyclerExpandActivity.this)
                    .inflate( R.layout.activity_recycler_main_list_item_child, null,false);

            return new ChildHolder(view);

        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            ChildHolder vh = (ChildHolder) holder;

            ChildBean childBean = mDatas.get(position);

            vh.tv_child.setText(childBean.getName());

        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }
    }


    class ParentHolder extends RecyclerView.ViewHolder {

        TextView tv_parent;
        RecyclerView recyclerViewChild;
        LinearLayout layout_layout_content;

        public ParentHolder(View itemView) {
            super(itemView);

            tv_parent = bindView(itemView, R.id.tv_parent);
            recyclerViewChild = bindView(itemView, R.id.recycler_child);
            layout_layout_content = bindView(itemView, R.id.layout_layout_content);
            recyclerViewChild.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
        }
    }


    class ChildHolder extends RecyclerView.ViewHolder {

        TextView tv_child;

        public ChildHolder(View itemView) {
            super(itemView);
            tv_child = bindView(itemView, R.id.tv_child);
        }
    }

}
